package com.fita.admin.service.authority.impl;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.CollectionUtils;
import com.fita.admin.dao.ResourceDao;
import com.fita.admin.service.DataServiceImpl;
import com.fita.admin.service.authority.ResourceService;
import com.fita.entity.authority.Resource;
import com.fita.entity.principal.Principal;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 
 * @author Ysy
 *
 */
@Service("resourceService")
public class ResourceServiceImpl extends DataServiceImpl<Resource> implements ResourceService {

    @Dao
    private ResourceDao resourceDao;
    
    /* (non-Javadoc)
     * @see com.fita.admin.service.authority.ResourceService#list(com.fita.entity.user.Principal)
     */
    @SuppressWarnings("unchecked")
    public Map<String, List<String>> list(Principal principal) {
        
        Map<String, List<String>> result = new HashMap<String, List<String>>();
        
        List<Resource> resources = resourceDao.list(principal.getAuthority());
        
        Map<String, List<Resource>> resourcesMap = CollectionUtils.constructMapValues(resources, "type");
        
        for(Entry<String, List<Resource>> item : resourcesMap.entrySet()){
            List<String> names = CollectionUtils.extractPropertyList(item.getValue(), "name");
            result.put(item.getKey(), names);
        }
            
        return result;
    }
    
}
